export default {
  state: () => ({
    cart: []
  }),
  mutations: {
    addGoods2cart(state, cartItem) {
      for (const item of state.cart) {
        if (item.goods.id === cartItem.goods.id) {
          item.num += cartItem.num
          return
        }
      }
      state.cart.push(cartItem)
    },
    delGoodsCart(state, index) {
      state.cart.splice(index, 1)
    },
    delCheckedGoodsCart(state) {
      state.cart.reduceRight((prev, item, index) => {
        if (item.checked) state.cart.splice(index, 1)

        return 0
      }, 0)
    },
    changeGoodsCartChecked(state, { index, flag }) {
      state.cart[index].checked = flag
    },
  },
  actions: {},
  getters: {}
}